var totalWidth = 20;
var headTable = "";
var dynamicData = "";
var endTable = "";


function drawFirstData() {

// Find total width of table
for (i=0; i<numColumn; i++) {
	totalWidth += Number(columnWidth[i]);
}
var adjustTotalWidth = (adjustWidth*numColumn);
//alert(Number(totalWidth)+Number(adjustTotalWidth));
 
// Head of table

headTable = "<TABLE width=\""+(Number(totalWidth)+Number(adjustTotalWidth))+"\"cellspacing=\"3\" cellpadding=\"0\" border=\"0\">"+
							"\n<TBODY>"+
							"\n<TR>"+
							"\n<TD width=20><INPUT type=\"checkbox\" name=\"checkAll\" onclick=\"selectAll(this)\"></TD>\n";
for (i=0; i<numColumn; i++) {
	headTable += "<TD align=\"center\" width=\""+(Number(columnWidth[i])+Number(adjustWidth))+"\""+
								"  style=\"color: white; font-family: 'Microsoft Sans Serif'; background-color: #7C7696; font-weight: bold; font-size: 9pt\">"+
								headText[i]+"</TD>\n";
}
headTable += "</TR>\n";
headTable += "</TBODY>\n</TABLE>\n";
dynamicHeadTable.innerHTML = headTable;


// Dynamic data
headTable = "<TABLE width=\""+(Number(totalWidth)+Number(adjustTotalWidth))+"\"cellspacing=\"3\" cellpadding=\"0\" border=\"0\">"+
							"\n<TBODY>";

dynamicData = "\n<TR>\n<TD width=20 align=center><INPUT type=\"checkbox\" name=\"check\" onclick=\"selectCheck(this)\"></TD>\n";
for (i=0; i<numColumn; i++) {
//	if (disable == undefined) {
//		dynamicData += "<TD align=\"center\" width=\""+columnWidth[i]+"\">"+
//			"<INPUT type=\"text\" style=\"width:"+(columnWidth[i])+"px; height:20px; text-align: center\" name=\"inputText"+(i+1)+"\" onkeydown=\"checkEnter()\">"+
//			"</TD>\n";
//	} else {
//alert(disable[i]);
		if(disable[i])
			disable[i] = "readonly";
		else
			disable[i] = "";
		dynamicData += "<TD align=\"center\" width=\""+columnWidth[i]+"\">"+
		"<INPUT type=\"text\"  style=\"width:"+(columnWidth[i])+"px; height:20px; text-align: center\" name=\"inputText"+(i+1)+"\" onkeydown=\"checkEnter()\">"+
		"</TD>\n";
//	}
}
dynamicData += "</TR>\n";

// End of table
endTable = "</TBODY>\n</TABLE>\n";

// Write all element
dynamicTable.innerHTML = headTable+dynamicData+endTable;
//alert(dynamicTable.innerHTML);
}

function addDynamicData() {

dynamicDatas = "";
numOfData = 1;
dynamicValues = new Array();
for (j=0; j<numColumn; j++) {
	dynamicValues[j] = new Array();
}

if (haveInputValue()) {

// Keep data from form
	if (frm.inputText1.value != undefined) {
		for (j=0; j<numColumn; j++) {
				dynamicValues[j][0] = eval("frm.inputText"+(j+1)).value;
		}
	} else {
		for (i=0; i<frm.inputText1.length; i++) {
			for (j=0; j<numColumn; j++) {
				dynamicValues[j][i] = eval("frm.inputText"+(j+1)+"["+i+"]").value;
			}
		}
	}

// Calculate number of data
	if (frm.check.length == undefined) {
		numOfData = 2;
	} else {
		numOfData = frm.check.length+1 ;
	}

// Generate Dynamic Table
	for (i=0; i<numOfData; i++) {
		dynamicDatas += dynamicData;
	}
dynamicTable.innerHTML = headTable+dynamicDatas+endTable;

// Return value to form
	if (frm.inputText1.value != undefined) {
		for (j=0; j<numColumn; j++) {
			eval("frm.inputText"+(j+1)).value = dynamicValues[j][0];
		}
	} else {
		for (i=0; i<frm.inputText1.length-1; i++) {
			for (j=0; j<numColumn; j++) {
				eval("frm.inputText"+(j+1)+"["+i+"]").value = dynamicValues[j][i] ;
			}
		}
	}

// Uncheck checkAll checkbox
frm.checkAll.checked = false;
focusField();
}

}

function deleteDynamicData() {

dynamicDatas = "";
numOfData = 1;
dynamicValues = new Array();
for (j=0; j<numColumn; j++) {
	dynamicValues[j] = new Array();
}

if (haveDeleteField()) {

// Keep data that not delete from form
for (j=0; j<numColumn; j++) {
	dynamicValues[j][0] = "";
}
k = 0;
	for (i=0; i<frm.check.length; i++) {
		if (frm.check[i].checked == false) {
			for (j=0; j<numColumn; j++) {
				dynamicValues[j][k] = eval("frm.inputText"+(j+1)+"["+i+"]").value;
			}
			k++;
		}
	}

// Calculate number of data
	numOfData = dynamicValues[0].length ;
// Generate Dynamic Table
	for (i=0; i<numOfData; i++) {
		dynamicDatas += dynamicData;
	}
dynamicTable.innerHTML = headTable+dynamicDatas+endTable;

// Return value to form
	if (frm.inputText1.value != undefined) {
		for (j=0; j<numColumn; j++) {
			eval("frm.inputText"+(j+1)).value = dynamicValues[j][0];
		}
	} else {
		for (i=0; i<frm.inputText1.length; i++) {
			for (j=0; j<numColumn; j++) {
				eval("frm.inputText"+(j+1)+"["+i+"]").value = dynamicValues[j][i] ;
			}
		}
	}

// Uncheck checkAll checkbox
frm.checkAll.checked = false;

//focusField();
}


}

function focusField() {
	setTimeout("frm.inputText1[frm.inputText1.length-1].focus();", 200);
}

function haveInputValue() {
var returnValue = true;
	if (frm.inputText1.value != undefined) {
		if (frm.inputText1.value == "" || haveOnlySpace(frm.inputText1)) {
			returnValue = false;
		}
	} else {
		if (frm.inputText1[frm.inputText1.length-1].value == "" || haveOnlySpace(frm.inputText1[frm.inputText1.length-1])) {
			returnValue = false;
		}
	}
return returnValue;
}

function haveDeleteField() {
var returnValue = false;
	if (!(frm.check.length == undefined || frm.check.length == 1)) {
		for (i=0; i<frm.check.length; i++) {
			if (frm.check[i].checked == true) {
				returnValue = true;
				break;
			}
		}
	}
return returnValue;
}

function haveOnlySpace(obj) {
var returnValue = true;
	if (obj.value != "") {
		for (i=0; i<obj.value.length; i++) {
			if (obj.value.charAt(i) != " ") {
				returnValue = false;
				break;
			}
		}
	}
return returnValue;
}

function selectAll(obj) {
	if (frm.check.length != undefined) {
		for (i=0; i<frm.check.length; i++) {
			frm.check[i].checked = obj.checked;		
		}
	} else {
		frm.check.checked = obj.checked;
	}
}

function selectCheck(obj) {
checkAll = true;
if (obj.checked) {
	if (frm.check.length != undefined) {
		for (i=0; i<frm.check.length; i++) {
			if (!frm.check[i].checked) {
				checkAll = false;
				break;
			}
		}
		frm.checkAll.checked = checkAll
	} else {
		frm.checkAll.checked = obj.checked;
	}
} else {
	frm.checkAll.checked = obj.checked;
}
}

function checkEnter() {
	if (event.keyCode == 13) {
		addDynamicData();
	}
}

function checkReturnValue(obj) {
	if (obj.value != "") {
		addValue(obj.value);
		obj.value = "";
	}
}

function addValue(values) {
lovValues = values.split(",")
dynamicDatas = "";
numOfData = 1;
dynamicValues = new Array();
for (j=0; j<numColumn; j++) {
	dynamicValues[j] = new Array();
}

// Keep data from form
	if (frm.inputText1.value != undefined) {
		for (j=0; j<numColumn; j++) {
				dynamicValues[j][0] = eval("frm.inputText"+(j+1)).value;
		}
	} else {
		for (i=0; i<frm.inputText1.length; i++) {
			for (j=0; j<numColumn; j++) {
				dynamicValues[j][i] = eval("frm.inputText"+(j+1)+"["+i+"]").value;
			}
		}
	}

/*
for (j=0; j<numColumn; j++) {
	dynamicValues[j][0] = "";
}
*/

// Keep new data from LOV to array;
	for (i=0; i<lovValues.length; i++) {
		for (j=0; j<dynamicValues[0].length; j++) {
			if (dynamicValues[0][j] == "") {
				dynamicValues[0][j] = lovValues[i];
				break;
			}
			if (lovValues[i] == dynamicValues[0][j]) {
				break;
			}
			if (j == (dynamicValues[0].length-1)) {
				presentArrayLength = dynamicValues[0].length;
				dynamicValues[0][presentArrayLength] = lovValues[i];
				for (k=1; k<numColumn; k++) {
					dynamicValues[k][presentArrayLength] = "";
				}
			}
		}
	}

// Calculate number of data
	numOfData = dynamicValues[0].length ;
// Generate Dynamic Table
	for (i=0; i<numOfData; i++) {
		dynamicDatas += dynamicData;
	}
dynamicTable.innerHTML = headTable+dynamicDatas+endTable;

// Return value to form
	if (frm.inputText1.value != undefined) {
		for (j=0; j<numColumn; j++) {
			eval("frm.inputText"+(j+1)).value = dynamicValues[j][0];
		}
	} else {
		for (i=0; i<frm.inputText1.length; i++) {
			for (j=0; j<numColumn; j++) {
				eval("frm.inputText"+(j+1)+"["+i+"]").value = dynamicValues[j][i] ;
			}
		}
	}

// Uncheck checkAll checkbox
frm.checkAll.checked = false;

}
